<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { addEmployee } from '@/api/employee'
import { FirstAidKit } from '@element-plus/icons-vue'

const router = useRouter()

// 表单数据
const registerForm = reactive({
  employeeName: '',
  password: '',
  confirmPassword: '',
  phone: '',
  role: 0 // 固定为管理员角色
})

// 表单规则
const rules = {
  employeeName: [
    { required: true, message: '请输入管理员姓名', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ]
}

// 表单引用
const registerFormRef = ref(null)
// 加载状态
const loading = ref(false)

// 注册方法
const handleRegister = async () => {
  // 表单验证
  await registerFormRef.value.validate()
  
  loading.value = true
  try {
    // 构造注册数据
    const employeeData = {
      employeeName: registerForm.employeeName,
      password: registerForm.password,
      role: registerForm.role, // 固定为管理员角色(0)
      phone: registerForm.phone
    }
    
    // 调用注册接口
    const res = await addEmployee(employeeData)
    if (res) {
      ElMessage.success('注册成功，请登录')
      router.push('/login')
    } else {
      ElMessage.error('注册失败，请稍后重试')
    }
  } catch (error) {
    console.error('注册失败', error)
    ElMessage.error('注册失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 返回登录页
const toLogin = () => {
  router.push('/login')
}
</script>

<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-left">
        <div class="register-title">
          <h1>连锁药房管理系统</h1>
          <p>Pharmacy Management System</p>
        </div>
      </div>
      
      <div class="register-right">
        <div class="register-form-header">
          <div class="logo-container">
            <el-icon :size="50" class="logo-icon">
              <component :is="FirstAidKit" />
            </el-icon>
          </div>
          <h2>管理员注册</h2>
          <p>注册成为系统管理员</p>
        </div>
        
        <el-form ref="registerFormRef" :model="registerForm" :rules="rules" label-width="0" class="register-form">
          <el-form-item prop="employeeName">
            <el-input 
              v-model="registerForm.employeeName" 
              placeholder="请输入管理员姓名"
              prefix-icon="User" 
              size="large"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input 
              v-model="registerForm.password" 
              placeholder="请输入密码" 
              type="password"
              prefix-icon="Lock"
              size="large"
              show-password
            />
          </el-form-item>
          
          <el-form-item prop="confirmPassword">
            <el-input 
              v-model="registerForm.confirmPassword" 
              placeholder="请再次输入密码" 
              type="password"
              prefix-icon="Lock"
              size="large"
              show-password
            />
          </el-form-item>
          
          <el-form-item prop="phone">
            <el-input 
              v-model="registerForm.phone" 
              placeholder="请输入手机号码"
              prefix-icon="Phone"
              size="large" 
            />
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              :loading="loading" 
              @click="handleRegister"
              size="large"
              class="register-button"
            >
              注册账号
            </el-button>
          </el-form-item>
          
          <div class="register-options">
            <el-button type="text" @click="toLogin">已有账号？立即登录</el-button>
          </div>
        </el-form>
        
        <div class="register-footer">
          <p>© {{ new Date().getFullYear() }} 连锁药房管理系统</p>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
}

.register-box {
  width: 900px;
  height: 600px;
  display: flex;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.1);
}

.register-left {
  flex: 1;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: #fff;
}

.register-title {
  text-align: center;
}

.register-title h1 {
  font-size: 32px;
  margin-bottom: 20px;
  font-weight: 600;
}

.register-title p {
  font-size: 16px;
  opacity: 0.8;
}

.register-right {
  flex: 1;
  background-color: #fff;
  padding: 40px;
  display: flex;
  flex-direction: column;
}

.register-form-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-container {
  margin-bottom: 20px;
}

.logo-icon {
  color: #1890ff;
  background-color: #e6f7ff;
  padding: 15px;
  border-radius: 50%;
}

.register-form-header h2 {
  font-size: 24px;
  margin-bottom: 10px;
  color: #333;
}

.register-form-header p {
  color: #909399;
  font-size: 14px;
}

.register-form {
  flex: 1;
}

.register-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
}

.register-options {
  margin-top: 15px;
  text-align: center;
}

.register-footer {
  text-align: center;
  margin-top: 20px;
  color: #909399;
  font-size: 12px;
}

/* 响应式调整 */
@media (max-width: 992px) {
  .register-box {
    width: 90%;
    flex-direction: column;
    height: auto;
  }
  
  .register-left {
    padding: 30px;
  }
  
  .register-right {
    padding: 30px;
  }
}
</style> 